<?
require("includes/funciones.inc.php");
require("includes/constants.php");
require("includes/selectLanguage.php");

session_start();
if(!isset($_SESSION['userName']))
	exit();

$DB = NewADOConnection('mysqlt');
$DB->Connect();
$DB->Execute("SET NAMES utf8;");
//$DB->debug=true;

$rsRecipe = $DB->Execute("SELECT * FROM recipes WHERE state <> '$stateErase' AND idRecipe=" . $_REQUEST["idRecipe"]); // execute query
if(!$rsRecipe->EOF){
	$recipesTitle = $rsRecipe->fields("description");
	$strSQL = "SELECT it.idRecipesItem, it.description, it.quantity, u.value, u.idUnit FROM recipes_items it INNER JOIN units u ON it.unit = u.idUnit WHERE idRecipe=" . $rsRecipe->fields("idRecipe");
	$rsRecipeItems = $DB->Execute($strSQL); // execute query
	
	$rsUnits = $DB->Execute("SELECT * FROM units"); // execute query
	while (!$rsUnits->EOF)
	{
		$strSelectUnits .= "<option value='" . $rsUnits->fields("idUnit") . "'>" . $a_languages[$rsUnits->fields("description")] . "</option>";
		$rsUnits->MoveNext();
	}
}
else {
	exit();
}
?>

<div id="mainPopUp">
	<div id="itmEdit">
		<div id="imgItmEdit">
			<?if(is_null($rsRecipe->fields("photo"))){?>
				<img src="imagenes/imgProd.png" id="imgRecipe" name="imgRecipe" width="177px" height="200px">
			<?}else{?>
				<img src="<?=$rsRecipe->fields("photo")?>" id="imgRecipe" name="imgRecipe" width="177px" height="200px">
			<?}?>
			<div id="fileUpload">
				<span id="fileUpload" class="itmIngBtnIn"><?=$a_languages['Recipes.ChangeImage']?></span>
			</div>
		</div>
		<div id="itmEditCont">
			<div id="itmEditTit"><?=$recipesTitle?></div>
			<ul id="itmList">
			<?while (!$rsRecipeItems->EOF) {?>
				<li>
					<button class="itmTableBtn btnDelItem" id="<?=$rsRecipeItems->fields("idRecipesItem")?>">X</button>
					<?
						if($rsRecipeItems->fields("idUnit") == $unitUnit)
							$descItem = $rsRecipeItems->fields("quantity").' '.$rsRecipeItems->fields("description");
						else
							$descItem = $rsRecipeItems->fields("quantity").$rsRecipeItems->fields("value").$a_languages['Common.Of'].$rsRecipeItems->fields("description");
						echo $descItem;
					?>
				</li>
			<?
				$rsRecipeItems->MoveNext();
				}
			?>	
			</ul>
			<form id="recipesItemsPopForm" name="recipesItemsPopForm">
				<input type="text" id="itemRescipesDescription" name="itemRescipesDescription" class="itmInput" value="">
				<select id="cmbUnits" name="cmbUnits" class="itmInput" style="width: 205px;">
					<?=$strSelectUnits?>
				</select>
				<input type="text" class="itmInput" id="itemRescipesQuantity" name="itemRescipesQuantity" value="" style="width: 50px;">
				<div class="clear"></div>
				<button id="btnItemExit" class="itmIngBtnIn"><?=$a_languages['Common.Cerrar']?></button>
				<button id="btnItemAdd" class="itmIngBtnIn"><?=$a_languages['Recipes.AddIngredient']?></button>
			</form>
		</div>
		<div class="clear"></div>
	</form>
</div>

<script>
	$(document).ready(function() {
		$_initPopItem();
	});
</script>